
Imports System
Imports System.Collections
Imports System.Data.SqlClient
Imports System.Diagnostics
Imports _2bMatesSite.Common

Namespace _2bMatesSite.BO
	''' <summary>
	''' Summary description for SpecialUser.
	''' </summary>
	Public Class SpecialUser
		Public ID As Integer
		Protected name As String
		Protected lastName As String
		Protected specialPageID As Integer
		Protected specialPages As ArrayList

		Public Sub New(ByVal ID As Integer, ByVal Name As String, ByVal LastName As String)
			Me.ID = ID
			Me.name = Name
			Me.lastName = LastName
			Me.specialPageID = -1
			Me.specialPages = New ArrayList()
		End Sub

		Public Property Name() As String
			Get
				Return name
			End Get

			Set
				name = value
			End Set
		End Property

		Public Property LastName() As String
			Get
				Return lastName
			End Get

			Set
				lastName = value
			End Set
		End Property

		Public Property SpecialPageID() As Integer
			Get
				Return specialPageID
			End Get

			Set
				specialPageID = value
			End Set
		End Property

		Public Overridable Property SpecialPages() As ArrayList
			Get
				Return specialPages
			End Get
			Set
				specialPages = value
			End Set
		End Property

		Public Function LoadUserSpecialPages() As ArrayList
			Dim specialUserPagesIDs As Hashtable = GetUsersSpecialPages()
			Dim list As New ArrayList()

			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()

			Try
				Dim cmd As New SqlCommand("SELECT * FROM SpecialPages ORDER BY Name ASC")
				cmd.Connection = conn

				Dim dr As SqlDataReader = cmd.ExecuteReader()

				Dim ordID As Integer = dr.GetOrdinal("ID")
				Dim ordName As Integer = dr.GetOrdinal("Name")

				While dr.Read()
					Dim usp As New UserSpecialPage(dr.GetInt32(ordID), dr.GetString(ordName))
					If specialUserPagesIDs.Contains(usp.spID) Then
						usp.IsMember = True
					Else
						usp.IsMember = False
					End If
					list.Add(usp)
				End While

				dr.Close()
			Finally
				conn.Close()
			End Try
			Return list
		End Function

		Protected Function GetUsersSpecialPages() As Hashtable
			Dim usp As New Hashtable()

			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()

			Try
				Dim cmd As New SqlCommand("SELECT SpecialPageID FROM SpecialUsers WHERE UID=@ID")
				cmd.Connection = conn
				cmd.Parameters.Add("@ID", ID)

				Dim dr As SqlDataReader = cmd.ExecuteReader()

				While dr.Read()
					usp(dr.GetInt32(dr.GetOrdinal("SpecialPageID"))) = dr.GetInt32(dr.GetOrdinal("SpecialPageID"))
				End While
				dr.Close()
			Finally
				conn.Close()
			End Try

			Return usp
		End Function

		Public Class UserSpecialPage
			Public spID As Integer
			Protected name As String
			Protected isMember As Boolean

			Public Sub New()
				spID = -1
				name = ""
				isMember = False
			End Sub

			Public Sub New(ByVal ID As Integer, ByVal name As String)
				Me.spID = ID
				Me.name = name
			End Sub

			Public Property Name() As String
				Get
					Return name
				End Get

				Set
					name = value
				End Set
			End Property

			Public Property IsMember() As Boolean
				Get
					Return isMember
				End Get

				Set
					isMember = value
				End Set
			End Property
		End Class

		Public Sub Save()
			DoInsert()
		End Sub

		Private Sub DoInsert()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Dim tran As SqlTransaction = conn.BeginTransaction()

			Try
				' Deelte all associated to this user special pages
				Dim cmd As New SqlCommand(GetDeleteSQL())
				cmd.Connection = conn
				cmd.Transaction = tran

				cmd.Parameters.Add("@UID", ID)

				Dim rows As Integer = cmd.ExecuteNonQuery()
				Debug.Assert(rows = 1)

				Dim specialPagesCount As Integer = specialPages.Count
				Dim i As Integer

				i = 0
				While i < specialPagesCount
					Dim tmpSpecialPage As UserSpecialPage = DirectCast(specialPages(i), UserSpecialPage)

					If tmpSpecialPage.IsMember Then
						cmd = New SqlCommand(GetInsertSQL())
						cmd.Connection = conn
						cmd.Transaction = tran

						cmd.Parameters.Add("@UID", ID)
						cmd.Parameters.Add("@SpecialPageID", tmpSpecialPage.spID)

						rows = cmd.ExecuteNonQuery()
						Debug.Assert(rows = 1)
					End If
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While

				tran.Commit()
			Catch e As Exception
				tran.Rollback()
				Throw e
			Finally
				conn.Close()
			End Try
		End Sub

		Private Shared Function GetInsertSQL() As String
			Dim sql As String = "INSERT INTO SpecialUsers" + Environment.NewLine
			sql += "( UID, SpecialPageID )" + Environment.NewLine
			sql += "VALUES" + Environment.NewLine
			sql += "( @UID, @SpecialPageID )" + Environment.NewLine
			Return sql
		End Function

		Private Shared Function GetDeleteSQL() As String
			Dim sql As String = "DELETE FROM SpecialUsers" + Environment.NewLine
			sql += "WHERE" + Environment.NewLine
			sql += "" & Chr(9) & "UID=@UID" + Environment.NewLine
			Return sql
		End Function

		Private Sub PopulateCommand(ByVal cmd As SqlCommand)
			DBManager.SetNZValue(cmd, "@UID", ID)
			DBManager.SetNZValue(cmd, "@SpecialPageID", specialPageID)
		End Sub

		Public Shared Function IsMemeber(ByVal id As Integer, ByVal specialPageID As Integer) As Boolean
			Dim tempSpecialUser As SpecialUser = GetSpecialUserByPageID(id, specialPageID)
			If tempSpecialUser <> Nothing Then
				Return True
			Else
				Return False
			End If
		End Function

		Public Shared Function GetSpecialUserByPageID(ByVal id As Integer, ByVal specialPageID As Integer) As SpecialUser
			Dim cmd As New SqlCommand("SELECT TOP 1 * FROM vSpecialUsers WHERE UID=@UID AND SpecialPageID=@SpecialPageID")
			cmd.Parameters.Add("@UID", id)
			cmd.Parameters.Add("@SpecialPageID", specialPageID)

			Return GetSpecialUserByCmd(cmd)
		End Function

		' Retrieve the special user using the given SQL command object
		' return null if no special user found
		Protected Shared Function GetSpecialUserByCmd(ByVal cmd As SqlCommand) As SpecialUser
			Dim su As SpecialUser = Nothing

			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()

			Try
				cmd.Connection = conn
				Dim dr As SqlDataReader = cmd.ExecuteReader()

				Dim ordID As Integer = dr.GetOrdinal("UID")
				Dim ordName As Integer = dr.GetOrdinal("Name")
				Dim ordLastName As Integer = dr.GetOrdinal("LastName")

				If dr.Read() Then
					su = New SpecialUser(dr.GetInt32(ordID), dr.GetString(ordName), dr.GetString(ordLastName))
					Debug.Assert(Not dr.Read())
				End If

				dr.Close()
			Finally
				conn.Close()
			End Try

			Return su
		End Function
	End Class
End Namespace






